package com.example.mongodb_learn;

import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.bson.Document;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicUpdate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.naming.ldap.BasicControl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;
import static org.springframework.data.mongodb.core.query.Update.update;

/**
 * MongoDbTemplate 操作mongodb数据库2
 */
@Component
public class MongoDbTemplateTest2 {
    @Resource
    private MongoTemplate mongoTemplate;


    /**
     * update更新语句
     */

    public void test1() {
        Query query = new Query(new Criteria("_id").is(4));
//        query.equals(4);

        Map<String, Object> userInfo = new HashMap<>();
        userInfo.put("age", 7);
        userInfo.put("dddd", "最底层");
        userInfo.put("sddddd", "vea");
        Document document = new Document(userInfo);
        BasicUpdate updateDefinition = new BasicUpdate(document);

        mongoTemplate.updateFirst(query, updateDefinition, "高三三班");
    }


    /**
     * update更新语句
     */

    public void test2() {

//        mongoTemplate.updateFirst(query(where("name").is("Joe")), update("age", 35), Student.class);
        UpdateResult updateResult = mongoTemplate.updateFirst(query(where("name").is("zkz5")), update("age", 2), "高三三班");
        System.out.println("更新记录条数:\t"+updateResult);
    }


    /**
     * uupdateFirst 更新满足条件的娣一条文档记录
     */

    public void test3() {

        UpdateResult updateResult = mongoTemplate.updateFirst(query(new Criteria()
                        .orOperator(where("age").is(2),where("name").is("zkz1"))),
                new Update().set("name", "zkz3").addToSet("jiamin","zkzddd").addToSet("jiamin","dddaa").push("dd","ddwa").push("dda",66), Student.class);
        System.out.println("更新记录条数:\t"+updateResult);
    }

    /**
     * updateMulti更新满足条件的全部文档数据
     * 匹配条件 age<=2或者name等于zkz1
     * 更新 name设置成zkz3
     */

    public void test4() {
        UpdateResult updateResult = mongoTemplate.updateMulti(query(new Criteria()
                        .orOperator(where("age").lte(2),where("name").is("zkz1"))),
                new Update().set("name", "zkz3").addToSet("jiamin","zkzddd").addToSet("jiamin","dddaa").push("dd","ddwa").push("dda",66), Student.class);
        System.out.println("更新记录条数:\t"+updateResult);
    }
    /**
     * 删除文档remove
     *
     */

    public void test5(){
        Student s =  new Student();
        s.setStuId(3);
        DeleteResult deleteResult = mongoTemplate.remove(s, "person");
        System.out.println("删除的记录条数为：\t"+deleteResult);

        DeleteResult deleteResult2 = mongoTemplate.remove((query(where("name").is("zkz3"))), "person");
        System.out.println("删除的记录条数为：\t"+deleteResult2);

        //删除文档并且返回删除的文档
        Student andRemove = mongoTemplate.findAndRemove((query(where("age").is(1))), Student.class);
        System.out.println("删除的记录条数为：\t"+deleteResult2);
    }


    /**
     * 删除文档findAndRemove只删除匹配的一条文档并且返回删除的文档
     *
     */

    public void test6(){
        //删除文档并且返回删除的文档
        Student andRemove = mongoTemplate.findAndRemove((query(where("age").is(1))), Student.class);
        System.out.println("删除的文档为：\t"+andRemove);
    }


    /**
     * 删除文档findAllandremovce
     *
     */

    public void test7(){
        //删除所有匹配文档并且返回所有删除的文档列表
        List<Object> allAndRemove = mongoTemplate.findAllAndRemove
                ((query(where("age").is(1))), "person");
        System.out.println("删除的文档为：\t"+allAndRemove);
    }
    /**
     * 查询文档
     */
    public void test8(){
        //如何查询文档中没有age属性的，不做匹配查询过滤该记录
        List<Map> list = mongoTemplate.find((query(where("age").lt(12))), Map.class,"高三三班");


        System.out.println(list);

    }

}
